﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using Maticsoft.DBUtility;
using TreeNode = FineUI.TreeNode;

namespace mydddd.sysModule
{
    public partial class List : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitLoad();
            
                bind();
            }
        }

        private void bind()
        {
            bind(-1);
        }

        private void bind(int treeid)
        {
           

            Grid1.PageSize = int.Parse(ddlPageSize.SelectedValue);
            string columns = @" parentID
                                ,moduleName
                                ,modulePath
                                ,moduleUrl
                                ,moduleIcon
                                ,sort
                                ,status
                                ,isend
                                ,info
                                ,id
                                 ";
            string whereSql = @"";

            if (treeid>= 0)
            {
                whereSql += string.Format(" and modulePath LIKE (SELECT s.modulePath+'%' FROM  sysModule s WHERE s.id={0})", treeid);
            }

            BindFineUI(columns, Grid1, "sysModule", whereSql,  "id"); 
            
            //BasePage  页面分页绑定方法
            //BindFineUI(columns, Grid1, "sysModule", whereSql,dic, "id"); 
        }

        #region tree 方法

        //绑定所有分类
        private void BindTree()
        {
            Tree1.Nodes.Clear();

             //添加“全部”节点
            //<f:TreeNode Text="全部" NodeID="-1" EnableCheckEvent="True" EnableCheckBox="True" Expanded="True" />
            TreeNode node = new TreeNode();
            node.NodeID = "-1";
            node.Text = "全部";
            node.EnableCheckBox = true;
            node.EnableCheckEvent = true;
        
            node.Expanded = true;//默认节点都为展开
            node.Checked = true;//默认节点都为选中

            Tree1.Nodes.Add(node);

            BLL.SysModule moduleBll = new BLL.SysModule();
            CreateTree(Tree1,
                moduleBll.GetAllList(), "moduleName",
                "moduleIcon", "info", "status");
        }

        /// <summary>
        /// 创建tree
        /// </summary>
        /// <param name="tree">tree控件ID</param>
        /// <param name="ds">数据集</param>
        /// <param name="nodeText">node显示的字段</param>
        /// <param name="nodeIcon">node显示的图标</param>
        /// <param name="nodeInfo">node显示的备注信息</param>
        /// <param name="show">是否显示</param>
        public static void CreateTree(FineUI.Tree tree, DataSet ds, string nodeText, string nodeIcon, string nodeInfo, string show)
        {
            DataRow[] drParent = ds.Tables[0].Select("parentID=0");

            foreach (DataRow dr in drParent)
            {
                TreeNode node = AddNode(dr, nodeText, nodeIcon, nodeInfo, show);

                tree.Nodes.Add(node);
                ResolveSubTree(ds, dr["id"].ToString(), node, nodeText, nodeIcon, nodeInfo, show);
            }
        }

        /// <summary>
        /// 绑定tree节点node
        /// </summary>
        /// <param name="dr">数据源</param>
        /// <param name="nodeText">显示文本的字段</param>
        /// <param name="nodeIcon">显示图标的字段</param>
        /// <param name="nodeInfo">显示备注消息的字段</param>
        /// <param name="show">显示是否隐藏的字段</param>
        /// <returns></returns>
        private static TreeNode AddNode(DataRow dr, string nodeText, string nodeIcon, string nodeInfo, string show)
        {
            TreeNode node = new TreeNode();
            node.NodeID = dr["id"].ToString();
            node.Text = dr[nodeText].ToString() + (dr[nodeInfo].ToString().Length > 0 ? "(" + dr[nodeInfo].ToString() + ")" : String.Empty) + (dr[show].ToString() == "1" ? String.Empty : "<font color='red'>隐藏</font>");
            if (dr[nodeIcon].ToString().Length>0)
                node.IconUrl = "/"+dr[nodeIcon].ToString();
            node.EnableCheckBox = true;
            node.EnableCheckEvent = true;
            //node.EnableClickEvent = true;//tree节点点击事件
            node.Expanded = true;//默认节点都为展开
            

            return node;
        }

        /// <summary>
        /// 循环子节点
        /// </summary>
        /// <param name="ds">数据集</param>
        /// <param name="parentID">父编号</param>
        /// <param name="nodeText">node显示的字段</param>
        /// <param name="nodeIcon">node显示的图标</param>
        /// <param name="nodeInfo">node显示的备注信息</param>
        /// <param name="show">是否显示</param>
        private static void ResolveSubTree(DataSet ds, string parentID, TreeNode treeNode, string nodeText, string nodeIcon, string nodeInfo, string show)
        {
            DataRow[] drParent = ds.Tables[0].Select("parentID=" + parentID + "");

            foreach (DataRow dr in drParent)
            {
                TreeNode node = AddNode(dr, nodeText, nodeIcon, nodeInfo, show);

                treeNode.Nodes.Add(node);
                ResolveSubTree(ds, dr["id"].ToString(), node, nodeText, nodeIcon, nodeInfo, show);
            }
        }

        protected void Tree1_OnNodeCheck(object sender, TreeCheckEventArgs e)
        {
            //设置单选
            foreach (FineUI.TreeNode node in Tree1.GetCheckedNodes())
            {
                node.Checked = node.NodeID == e.Node.NodeID;
                //if (node != e.Node)
                //{
                //    node.Checked = false;
                //}
            }

            bind(int.Parse(e.Node.NodeID));
        }

        #endregion

        #region gird常用方法

        //初始加载
        private void InitLoad()
        {
            BindTree();
        }


        //添加
        protected void Button_add_OnClick(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(
                   Window1.GetShowReference("sysModuleAdd.aspx", "添加"));

        }

        //删除
        protected void Button_delete_OnClick(object sender, EventArgs e)
        {
            string ids = GetDataKeysBySelectedRow(Grid1);
            if (ids.Length == 0)
            {
                Alert.Show("请选择记录！");
                return;
            }
            mydddd.BLL.SysModule bll = new mydddd.BLL.SysModule();
            bll.DeleteList(ids);

            BindTree();

            bind();
        }

        //每页显示数目
        protected void ddlPageSize_OnSelectedIndexChanged(object sender, EventArgs e)
        {
            bind();
        }

        //分页事件
        protected void Grid1_OnPageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            bind();
        }
      
      
        #endregion

        protected void Window1_OnClose(object sender, WindowCloseEventArgs e)
        {
            BindTree();

            bind();
        }

        #region 右键菜单事件
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_add_OnClick(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_edit_OnClick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_del_OnClick(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
        }

        #endregion
    }
}